
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use work.state_pkg.all;

entity track_state_ctrl is
port (	clk			: in	std_logic;
		reset			: in	std_logic;

		sensor_l		: in	std_logic;
		sensor_m		: in	std_logic;
		sensor_r		: in	std_logic;
		
		continu : out std_logic;
		rover_direction : out direction_type;
		
		dbm : out string (2 downto 1)
	);
end track_state_ctrl;

architecture Behavioral of track_state_ctrl is

		type		track_state_ctrl_state is ( 	rest,
															forward,
															right,
															sharp_right,
															left,
															sharp_left,
															end_state);
															
		signal state, new_state:			track_state_ctrl_state;
		signal sensor: std_logic_vector(2 downto 0);

begin
			sensor <= sensor_l & sensor_m & sensor_r;
			
			process (clk)
			begin
						if (rising_edge (clk)) then
								if ( reset = '1') then
										state <= rest;
								else
										state <= new_state;
								end if;
						end if;
			end process;						

			process (state, sensor)
			begin
						case state is
								when rest =>
										continu					<= '1';
										rover_direction		<= STOP;
										dbm						<= "RE";
							  
											if (sensor = "010" or sensor = "101") then
													new_state   <= forward;
											elsif (sensor = "001") then
													new_state   <= left;
											elsif (sensor = "011") then
													new_state   <= sharp_left;
											elsif (sensor = "100") then
													new_state   <= right;
											elsif (sensor = "110") then
													new_state   <= sharp_right;
											else
													new_state   <= end_state;
											end if;
											
								when forward =>
								 		continu					<= '1';
										rover_direction		<= STRAIGHT;
										dbm						<= "FO";
							  
											if (sensor = "010" or sensor = "101") then
													new_state   <= forward;
											elsif (sensor = "001") then
													new_state   <= left;
											elsif (sensor = "011") then
													new_state   <= sharp_left;
											elsif (sensor = "100") then
													new_state   <= right;
											elsif (sensor = "110") then
													new_state   <= sharp_right;
											else
													new_state   <= end_state;
											end if;
											
								 when right =>
								 		continu					<= '1';
										rover_direction		<= SOFT_RIGHT;
										dbm						<= "SR";
							  
											if (sensor = "010" or sensor = "101") then
													new_state   <= forward;
											elsif (sensor = "001") then
													new_state   <= left;
											elsif (sensor = "011") then
													new_state   <= sharp_left;
											elsif (sensor = "100") then
													new_state   <= right;
											elsif (sensor = "110") then
													new_state   <= sharp_right;
											else
													new_state   <= end_state;
											end if;
											
									when sharp_right =>
								 		continu					<= '1';
										rover_direction		<= HARD_RIGHT;
										dbm						<= "HR";
							  
											if (sensor = "010" or sensor = "101") then
													new_state   <= forward;
											elsif (sensor = "001") then
													new_state   <= left;
											elsif (sensor = "011") then
													new_state   <= sharp_left;
											elsif (sensor = "100") then
													new_state   <= right;
											elsif (sensor = "110") then
													new_state   <= sharp_right;
											else
													new_state   <= end_state;
											end if;
										
									when left =>
								 		continu					<= '1';
										rover_direction		<= SOFT_LEFT;
										dbm						<= "SL";
							
							  
											if (sensor = "010" or sensor = "101") then
													new_state   <= forward;
											elsif (sensor = "001") then
													new_state   <= left;
											elsif (sensor = "011") then
													new_state   <= sharp_left;
											elsif (sensor = "100") then
													new_state   <= right;
											elsif (sensor = "110") then
													new_state   <= sharp_right;
											else
													new_state   <= end_state;
											end if;
									
									when sharp_left =>
								 		continu					<= '1';
										rover_direction		<= HARD_LEFT;
										dbm						<= "HL";
							  
											if (sensor = "010" or sensor = "101") then
													new_state   <= forward;
											elsif (sensor = "001") then
													new_state   <= left;
											elsif (sensor = "011") then
													new_state   <= sharp_left;
											elsif (sensor = "100") then
													new_state   <= right;
											elsif (sensor = "110") then
													new_state   <= sharp_right;
											else
													new_state   <= end_state;
											end if;
											
									when end_state =>
								 		continu					<= '0';
										rover_direction		<= STOP;
										dbm						<= "ES";
							         new_state				<= end_state;
										
						end case;
			end process;
end architecture behavioral;
							
																

											


